﻿// Custom example logic
var curDirId = 0;
var curDirName = "";
var curDirSid = -1;
var curDirSname = "";
var curDirScount = 0;
var gSt = 0;
var uploader;
var gFileArr = new Array();
var gInterval;
var gIsEditSubject = false;
var gIsEditSubjectOnly = false;
function initUploader()
{
	uploader = new plupload.Uploader({
	runtimes : 'html5,flash,silverlight,html4',
	browse_button : 'pickfiles', // you can pass in id...
	container: document.getElementById('dialog-form'), // ... or DOM Element itself
	url : 'upload.php',
	flash_swf_url : 'js/Moxie.swf',
	silverlight_xap_url : 'js/Moxie.xap',
	chunk_size:"2mb",
	max_retries:1000,
	filters : {
		max_file_size : '10000mb',
		mime_types: [
			{title : "Image files", extensions : "jpg,gif,png,tiff,tif"},
			{title : "Zip files", extensions : "zip,rar"},
			{title : "Video files", extensions : "mp4,mov,avi,mpeg,mpg,flv"},
			{title : "Sound files", extensions : "mp3,wav"},
			{title : "Doc files", extensions : "doc,ppt,xls,docx,pptx,xlsx,psd,ai,pdf,plist,ipa"}
		]
	},

	init: {
		PostInit: function() {
			document.getElementById('filelist').innerHTML = ''; 
		},

		FilesAdded: function(up, files) {
			plupload.each(files, function(file) {
				//initProgrssBar();
				var html = $("#divTempFileHtml").html();
				html = html.replace("TBLID","tblFile_"+file.id);
				var str = file.name;
				if(str.length>30)
				{
					str = str.substring(0,30)+"...";
				}
				html = html.replace("NAME",str);
				html = html.replace("SIZE",plupload.formatSize(file.size));
				document.getElementById('filelist').innerHTML += "<li>"+html+"</li>";
				//alert(html);
				$("#tblFile_"+file.id).find("#progressbar").progressbar({value:0});
				if($("#filelist").is(":hidden"))
				{
					$("#filelist").show();
				}
				//uploader.start();
				//document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
				//document.getElementById('filelist').innerHTML += '<table width="100%" class="tblFile" id="tblFile_'+file.id +'"><tr><td width="10" rowspan="2">&nbsp;</td><td width="350">'+file.name+'</td><td width="100">'+plupload.formatSize(file.size)+'</td><td  width="223" rowspan="2">&nbsp;</td></tr><tr><td colspan="2" id="tdProess_'+file.id +'">&nbsp;</td></tr></table><br/>'
			});
		},

		UploadProgress: function(up, file) {
			//document.getElementById("tdProess_"+file.id).innerHTML = '<span>' + file.percent + "%</span>";
			$("#tblFile_"+file.id).find("#uploadRate").html(file.percent+"%");
			$("#tblFile_"+file.id).find("#progressbar").progressbar({value:file.percent});
			
			//$("#tblFile_"+file.id).find("#progressbar").progressbar("value",file.percent);
			//document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
		},
        FileUploaded:function(up,file,o)
		{
			
			//$("#dialog-confirm").dialog("open");
			var json = eval("("+o.response+")");
			$("#tblFile_"+file.id).find("#imgOk").show();
			gFileArr[file.id] = false;
			$.post("do.php",{name:file.name,path:json.path},function(data){
				//alert(data);
				try
				{
				var j= eval("("+data+")");
				if(j.st=="ok")
				{
					gFileArr[j.fid] = true;
				}
				else
				{
					alert(j.msg);
				}
				}
				catch(e){}
				});
		},
		UploadComplete:function(up,o)
		{
			alert("全部上传完毕");
			//gInterval = setInterval("checkFinish()",1000);
		},
		Error: function(up, err) {
			document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
		}
	}
});
	uploader.init();
}
function checkFinish()
{
	var haveAllTure = true;
	for(var item in gFileArr)
	{
	    if(!gFileArr[item])
		{
			haveAllTure = false;
			break;
		}
	}
	if(haveAllTure)
	{
		clearInterval(gInterval);

		//alert("上传完毕");
		$("#dialog-confirm").find("#msg").html("处理完毕");
		$("#dialog-confirm").dialog("close");
 
	}
}
function initProgrssBar(jo)
{
	var progressbar = jo;
	progressbar.progressbar({
      value: 0
    });
	 
}
$(function() {
	newUploader("tdShowImg");
 
		$( "input[type=submit], button" )
			.button()
			.click(function(event) {
			    var bid = $(event.target)[0].id;
				if(bid=="")
				{
					bid = $(event.target).parent()[0].id;
				}
	           // alert(bid);
			    $("#name").val("");
                if(bid=="createUser")
			    {
					if(refreshUpload())
					{
						$("#dialog-form").dialog("open");
					}
			    }
				else if(bid=="btnDelete")
				{
					doDelFile();
				}
				else if(bid=="btnSetDir")
				{
					setDir();
				}
				else if(bid=="diradd" || bid=="dirrename" || bid=="dirdel")
				{
					$("#dialog-form-dir").dialog("open");
				}
				else if(bid=="btnRemoveDir")
				{
					removeDir();
				}
				else if(bid=="btnModify")
				{
					toModifyPage();
				}
				else if(bid=="btnDetailSave")
			    {
					if($("#mark").val()=="")
					{
						alert("请填写名称");
						$("#mark")[0].focus();
						return;
					}
					if($("#avaliDate").val()=="")
					{
						alert("请选择有效期限");
						return;
					}
					if($("#month").val() == "0" || $("#year").val()=="0")
					{
						alert("请选择创建时间");
						$("#year")[0].focus();
						return;
					}
					$("#hidyear").val($("#year").val()+"-"+$("#month").val());
					if($("#tblSelCar").find("tr").length>0)
					{
					var carIds = ",";
                    var carNames = ",";
					$("#tblSelCar").find("tr").each(function(){
					    carIds = carIds+$(this).attr("data")+",";
						var n = $(this).find("td")[0].innerHTML;
						carNames = carNames+ n.replace(" ",":")+",";
					});
					$("#carIds").val(carIds);
					$("#carNames").val(carNames);
					}
					else
					{
						$("#carIds").val("");
						$("#carNames").val("");
					}
					if($("#tblSelType").find("tr").length>0)
					{
					var typeIds = ",";
					var typeNames = ",";
					$("#tblSelType").find("tr").each(function(){
					    typeIds = typeIds+$(this).attr("data")+",";
						var n = $(this).find("td")[0].innerHTML;
						typeNames = typeNames+ n.replace(" ",":")+",";
					});
					$("#typeIds").val(typeIds);
					$("#typeNames").val(typeNames);
					}
					else
					{
					$("#typeIds").val("");
					$("#typeNames").val("");
					}

					if($("#id").val().indexOf(",")!=-1)
					{
						if(confirm("您已选择多个文件，如果继续，则会覆盖所选文件已有的信息，是否继续？"))
						{
							$("#optype").val("save");
							$("#form").submit();
						}
					}
					else
					{
						$("#optype").val("save");
						$("#form").submit();
					}
				}
				else if(bid=="btnDetailPost")
				{
					if($("#id").val().indexOf(",")!=-1 || $("#pdid").val()!="0")
					{
						if(confirm("您已选择多个文件，如果继续，则会覆盖所选文件已有的信息，是否继续？"))
						{
							$("#optype").val("post");
							$("#form").submit();
						}
					}
					else
					{
						$("#optype").val("post");
						$("#form").submit();
					}
				}
				else if(bid=="btnDetailUnPost")
				{
					if($("#id").val().indexOf(",")!=-1 || $("#pdid").val()!="0")
					{
						if(confirm("您已选择多个文件，如果继续，则会下架所选文件，是否继续？"))
						{
							$.get("?act="+$("#act").val()+"&op=saveInfo&isPost=-1&id="+$("#id").val()+"&did="+$("#pdid").val()+"&src=ajax",function(data){
								alert("下架成功");
							});
						}
					}
					else
					{
						$.get("?act="+$("#act").val()+"&op=saveInfo&isPost=-1&id="+$("#id").val()+"&src=ajax",function(data){
						alert("下架成功");
						});
					}
				}
				else if(bid=="btnUnPost")
				{
					doUnPost();
				}
				else if(bid=="btnPost")
				{
					//doPost();
					 if(curDirSid==-1 || curDirSid==0 || curDirSname=="")
					{
						alert("请先在左侧选择目录下的某个主题，再点击完善备注");return;
					}
					toModifyPage();
				}
				else if(bid=="btnShowDirForm")
				{
					if($("#id").val().indexOf(",")!=-1)
					{
						if(confirm("您已选择多个文件，如果继续，则会删除所选文件已所属的目录，是否继续？"))
						{
							$("#dialog-form-adddir").dialog("open");
							 
						}
					}
					else
					{
						$("#dialog-form-adddir").dialog("open");
					}
				}
				else if(bid=="btnAddApply")
				{
					 doAddApplyList();
				}
				return false;
				//alert("[pppp");
		});
		
		$( "#dialog-confirm" ).dialog({
			autoOpen: false,
			height:200,
			width: 300,
			modal: true,
			close: function() {
				  return false;
			},
			open:function(event, ui){
				$(".ui-dialog-titlebar-close", $(this).parent()).hide();
				}
		});
		$( "#dialog-form" ).dialog({
			autoOpen: false,
			height:450,
			width: 500,
			modal: true,
			buttons: {
				"开始上传": function() {
 					uploader.start();
				},
				"关闭": function() {
 					$( "#dialog-form" ).dialog("close");
				}

			},
			close: function() {
				
				 $("#chkSelAll")[0].checked=false;
				 gSt = 0;
				showStFileCount(curDirId,curDirSid);
				 //getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
			}
		});
		$( "#dialog-form-dir" ).dialog({
			autoOpen: false,
			height: 200,
			width: 350,
			modal: true,
			buttons: {
				"保存": function() {
 					saveDir();
				},
				"关闭": function() {
					 $("#dialog-form-dir").dialog("close");
				}
			},
			close: function() {
			
			}
		});
		 $( "#dialog-form-adddir" ).dialog({
			autoOpen: false,
			height: 220,
			width: 250,
			modal: true,
			buttons: {
				"保存": function() {
					 
 					saveFileType();
				},
				"关闭": function() {
					 $("#dialog-form-adddir").dialog("close");
				}
			},
			close: function() {
			
			}
		});		
	 
		//getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
		
		$("#chkSelAll").change(function(){
			 if(this.checked)
			 {
				 $("#ulList").find("input").each(function(){
					 if(this.disabled==false)
					 {
					 	this.checked = true;
					 }
					 });
			 }
			 else
			 {
				 $("#ulList").find("input").each(function(){
					 this.checked = false;
					 });
			 }
			});
			
		$("#chkDirAll").change(function(){
			 if(this.checked)
			 {
				 $("#ulDir").find("input").each(function(){
					 if(this.disabled==false)
					 {
					 	this.checked = true;
					 }
					 });
			 }
			 else
			 {
				 $("#ulDir").find("input").each(function(){
					 this.checked = false;
					 });
			 }
			});			
	 
		initUploader();
		 
});
function refreshUpload()
{
	 
	$("#editId").val("");
	//$("#ui-id-1").html("添加超大附件");
	$("#filelist").hide();
	 
	document.getElementById('filelist').innerHTML ="";
 	uploader.refresh();
	return true;
}
function doAddApplyList()
{
	 if(gSt!=3)
	 {
		return;
	 }
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
			 gId = this.value;
		 }
		 });
     
     if(id=="")
	 {
		 if(confirm("您没有选择文件，是否要把当前目录 \""+curDirName+"\" 当前主题 \""+curDirSname+"\" 已完善信息所有文件加入到待审批列表？"))
		 {
			 gid = 0;
			 $("#id").val("0");
			 $("#pdid").val(curDirId);
			 $("#fpdid").val(curDirId);
		 }
		 else
		 {
			 return;
		 }
	 }
	 gId = id.substring(0,id.length-1);
	 $.post("?act=upload&op=doAddApplyList",{id:gId,dirId:curDirId,sid:curDirSid},function(d){
		 $("#pdid").val("0");
		 var j = eval("("+d+")");
		 if(j.st=="ok")
		 {
			// alert("文件已加入待审批列表 请查阅");
			headerShowTips("文件已加入审批列表，是否选择文件发送客户审批？",3)
		 }
		 else
		 {
			 alert("加入失败");
		 }
		 
	});
}
function showListBySt(obj)
{
			$("#btnAddApply").css("background","#888");
			$("#btnUnPost").css("background","#888");
			gSt = obj.value;
			if(gSt==0)
			{
				//$("#btnDelete").show();
				//$("#btnPost").show();
				//$("#btnUnPost").hide();
		 
			}
			else if(gSt==1)
			{
				//$("#btnUnPost").show();
				//$("#btnPost").hide();
				//$("#btnDelete").hide();
				$("#btnUnPost").css("background","#597DDB");
			}
            else if(gSt==3)
			{
				$("#btnAddApply").css("background","#597DDB");
			}

			else if(gSt==2)
			{
				//$("#btnPost").hide();
				//$("#btnDelete").hide();
				//$("#btnUnPost").hide();
			}
			
			getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
		 
}
function doPost()
{
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
		 }
		 });
     if(id=="")
	 {
		 var msg = "您没有选择文件，是否要完善当前目录 \""+curDirName+"\" 所有文件的信息 包含所有主题下的所有文件及主题信息？";
		 if(curDirSname!="")
		 {
			 msg = "您没有选择文件，是否要完善当前目录 \""+curDirName+"\" 主题 \""+curDirSname+"\" 所有文件及主题的信息？";
		 }
		 if(!confirm())
		 {
			 return;
		 }

	 }
	 id = id.substring(0,id.length-1);
	$.post("?act=upload&op=saveInfo&src=ajax",{id:id,optype:"post"},function(d){
		 var j = eval("("+d+")");
		 if(j.st=="ok")
		 {
			 alert("发布成功");
			 getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
		 }
	 });
}
function doUnPost()
{
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
		 }
		 });
     if(id=="")
	 {
		 alert("请选择要下架的文件");return;
	 }
	 if(confirm("确定要下架选中的文件吗？"))
	 {
	 id = id.substring(0,id.length-1);
 
 
	 $.post("?act=upload&op=saveInfo&src=ajax",{id:id,isPost:"-1",dbsid:curDirSid},function(d){
		 var j = eval("("+d+")");
		 if(j.st=="ok")
		 {
			 alert("下架成功");
			 //getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
		 	 gSt =2;
			showStFileCount(curDirId,curDirSid);
		 }
	 });
	 }
	
}
function removeDir()
{
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
		 }
	 });
     if(id=="")
	 {
		 alert("请选择要从当前目录 "+curDirName+" 移出的文件");return;
	 }
 	if(confirm("确定要把所选文件从目录 "+curDirName+" 移出吗？"))
	 {
	 id = id.substring(0,id.length-1);
	 $.post("?act=upload&op=ajaxRemoveFile",{id:id,did:curDirId},function(d){
		 var j = eval("("+d+")");
		 if(j.st=="ok")
		 {
			 alert("操作成功");
			 getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
		 }
	 });
	 }
}
function setDir()
{
	 var did = "";
	 $("#ulDir").find("input").each(function(){
		 if(this.checked)
		 {
			 did = did+this.value+",";
		 }
		 });
     if(did=="")
	 {
		 alert("请选择要存放的目录");return;
	 }
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
		 }
		 });
     if(id=="")
	 {
		 alert("请选择要存放的文件");return;
	 }
	 if(confirm("确定执行此操作吗？"))
	 {
	 id = id.substring(0,id.length-1);
	 did = did.substring(0,did.length-1);
	 $.post("?act=upload&op=ajaxDirFile",{id:id,did:did},function(d){
		 var j = eval("("+d+")");
		 if(j.st=="ok")
		 {
			 alert("操作成功");
			// getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
		 }
	 });
	 }
}
function doDelFile()
{
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
		 }
		 });
     if(id=="")
	 {
		 alert("请选择要删除的文件");return;
	 }
	 if(confirm("确定要删除选中的文件吗？"))
	 {
	 id = id.substring(0,id.length-1);
	 $.post("?act=upload&op=ajaxDel",{id:id},function(d){
		 var j = eval("("+d+")");
		 if(j.st=="ok")
		 {
			 alert("成功删除");
			 getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
		 }
	 });
	 }
}
function saveDir()
{
	if($("#dirName").val()=="")
	{
		alert("目录名称不能为空");
		return;
	}
	$.post("?act=upload&op=ajaxSaveDir",{name:$("#dirName").val(),id:$("#dirId").val()},function(d){
 
		if(d.indexOf("ok")!=-1)
		{
			lstDir();
		}
		else if(d.indexOf("exist")!=-1)
		{
			alert("已经存在");
		}
		});
}
function saveFileType()
{
   //$("#fromFyleType")[0].submit();
 
   if($("#brand").val()!=0 || $("#car").val()!=0)
   {
	   var show;
	   if($("#car").val()==0)
	   {
		   show = $("#brand").find("option:selected").text()+' ';
	   }
	   else
       {
		   show = $("#brand").find("option:selected").text()+' '+ $("#car").find("option:selected").text();
	   }
	   var isNoHave = true;
	   $("#tblSelCar").find("td").each(function(){
	       if($(this).text().indexOf(show)!=-1)
		   {
			   isNoHave = false;
			   return;
		   }
	   });
	   if(isNoHave)
	   {
	   var idx =  $("#tblSelCar").find("tr").length+1;
	   $("#tblSelCar").append('<tr data="'+$("#brand").val()+":"+$("#car").val()+'"><td style="padding:0; border:none;width:200px; padding-left:20px;">'+show+'</td><td style="font-size:20px;padding:0; border:none;cursor:pointer"  onclick="delFileType(2,'+idx+')">x</td></tr>');
	   $("#dirCars").html("—品牌车型&nbsp;("+$("#tblSelCar").find("tr").length+")");
	   }
   }
   if($("#type").val()!=0 || $("#style").val()!=0)
   {
	   var show;
	  
	   if($("#style").val()==0)
	   {
		   show = $("#type").find("option:selected").text()+' ';
	   }
	   else
	   {
		   show = $("#type").find("option:selected").text()+' '+ $("#style").find("option:selected").text();
	   }
	   var isNoHave = true;
	   $("#tblSelType").find("td").each(function(){
	       if($(this).text().indexOf(show)!=-1)
		   {
			   isNoHave = false;
			   return;
		   }
	   });
	   if(isNoHave)
	   {
	   		var idx =  $("#tblSelType").find("tr").length+1;
	   		$("#tblSelType").append('<tr data="'+$("#type").val()+":"+$("#style").val()+'"><td style="padding:0; border:none;width:200px; padding-left:20px;">'+show+'</td><td style="font-size:20px;padding:0; border:none; cursor:pointer" onclick="delFileType(1,'+idx+')">x</td></tr>');
	   $("#dirTypes").html("—内容类型&nbsp;("+$("#tblSelType").find("tr").length+")");
	   }
   }
}
function lstDir()
{
	getPageDir("?act=upload&op=ajaxLstDir&ajax=getPageDir");
}
function getPageDir(url)
{
	$.get(url,function(data){
		    $("#ulDir").html("");
		    var j = eval("("+data+")");
			if(j.data.length>0)
			{
				for(i=0;i<j.data.length;i++)
				{
					if(i==0 && curDirId==0)
					{
						curDirId = j.data[i].id;
						curDirName = j.data[i].name;
					}
					var name = j.data[i].name;

					$("#ulDir").append('<li title="'+j.data[i].name+'"><input type="checkbox" value="'+j.data[i].id+'"/><div id="dirId_'+j.data[i].id+'" onclick="showFile(this,'+j.data[i].id+',\''+j.data[i].name+'\');">'+name+'</div><table cellpadding="0" cellspacing="0" id="tblDirGroup"></table></li>');
				}
				//alert(curDirId);
				if(curDirId==0)
				{
					showFile($($("#ulDir").find("li")[0]).find("div")[0],curDirId,curDirName);
				}
				else
				{
					showFile($("#dirId_"+curDirId)[0],curDirId,curDirName);
				}
				$("#selSt")[0].disabled= true;
				$("#chkSelAll")[0].disabled = true;
			}
			//alert(j.pageNav);
			$("#dirPageNav").html(j.pageNav);
			
			});
}
function showFile(o,id,name)
{
     $("#btnAddFengMian").find("span").html("1.新建主题");
	$("#chkSelAll")[0].checked = false;
	$("#ulDir").find("li").each(function(){
	   $(this).find("div").css("background-color","#FFFFFF");
	});
	$(o).css("background-color","#CCC");
	//if(id=="0")
	//{
	//	$("#title").html('全部目录');
	//	$("#btnPost").hide();
	//}
	//else
	//{
	//	$("#btnPost").show();
	//	$("#title").html("目录："+name);
	//}
	curDirId = id;
	curDirName = name;
	curDirSid = -1;
	curDirSname="";
	showDirGroups(o,id);
	showStFileCount(id,curDirSid);
	$("#selSt")[0].disabled = true;
	//showListBySt();
	//$("#selSt").val(0);
	//$("#selSt").change();
	//getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
}
function showDirGroups(o,id)
{
	$("#ulDir").find("li").each(function(){
		$(this).find("#tblDirGroup").slideUp(100);
		});
	$.get("?act=upload&op=getDirGroup&id="+id,function(d){
	    var j = eval("("+d+")");
		if(j.data.length>0)
		{
			var tblData = $(o).parent().find("#tblDirGroup");
			//alert(tblData.html());
			tblData.html("");
			var lastId="";
			var lastName = "";
			var lastCount = 0;
			for(i=0;i<j.data.length;i++)
			{
				j.data[i].name = j.data[i].name.replace(/[\r\n]/g,"");
				var show = j.data[i].name;
				var style = "";
				if(i==0)
				{
					show = j.data[i].name+" ("+j.data[i].count+")";
					style='style="color:#597DDB;font-weight:bold;"';
				}
				else if(j.data[i].t==0)
				{
					style='style="color:red;font-weight:bold;" title="缺少分类"';
				}
				//show = show.replace(/[\r\n]/g,"");
				tblData.append('<tr><td '+style+' id="tdDirSubjectId_'+j.data[i].id+'" onclick="clickSubject(this,'+j.data[i].id+',\''+j.data[i].name+'\','+j.data[i].count+')">·'+show+'</td></tr>');
				lastId = j.data[i].id;
				lastName = j.data[i].name;
				lastCount = j.data[i].count;
			}
			tblData.slideDown(100);
			if(curDirSid!=-1)
			{
				clickSubject($("#tdDirSubjectId_"+curDirSid)[0],curDirSid,curDirSname,curDirScount);
			}
			if(gShowSubjectSelect)
			{
				clickSubject($("#tdDirSubjectId_"+lastId)[0],lastId,lastName,lastCount);
				gShowSubjectSelect=false;
			}
			//clickSubject(tblData.find("td")[0],j.data[0].id,j.data[0].name,j.data[0].count)
		}
	});
	
}
function clickSubject(o,id,name,num)
{
   				$("#selSt")[0].disabled= false;
				$("#chkSelAll")[0].disabled = false;
   if(id>0)
   {
   	    $("#btnAddFengMian").find("span").html("1.编辑主题");
   }
   else
   {
	   $("#btnAddFengMian").find("span").html("1.新建主题");
   }
   gSt = 0;
   curDirSid = id;
   curDirSname = name;
   curDirScount = num;
   showStFileCount(curDirId,id);
	$("#chkSelAll")[0].checked = false;
	$(o).parent().parent().parent().parent().find("div").css("background-color","#FFFFFF");
	$(o).parent().parent().find("td").each(function(){
	   $(this).css("background-color","#FFFFFF");
	});
	$(o).css("background-color","#EEE");
	//$("#title").html("目录："+curDirName+'-'+name);
	 
	//showListBySt();

}
function getPage(url)
{
		var sid = curDirSid;
 
		$.get(url+"&src=edit&sid="+sid,function(data){
		var j = eval("("+data+")");
		if(j.st=="ok")
		{
		 
			$("#ulList").html("");
			if(j.data.total>0)
			{
				var ul = "";
				for(i=0;i<j.data.data.length;i++)
				{
					var name = j.data.data[i].name;
					
					var name1 = name;
					var name2 = "";
					if(name.indexOf("<br/>")!=-1)
					{
						var names = name.split("<br/>");
					 
						if(names[0].length>8)
						{
							name1 = names[0].substring(0,8)+"..";
						}
					    else
						{
							name1 = names[0];
						}
						if(names[1].length>10)
						{
							name2 = names[1].substring(0,10)+"..";
						}
						else
						{
							name2 = names[1];
						}
					}
					else
					{
						if(name1.length>8)
						{
							name1 = name1.substring(0,8)+"..";
						}
					}
					 
					ul = ul+'<li><img src="'+j.data.data[i].pic+'" onClick="showDetail('+j.data.data[i].id+');"/><div title="'+j.data.data[i].mark+'">'+name1+'<br/>'+name2+'</div><input type="checkbox" id="chk" value="'+j.data.data[i].id+'"/></li>';
					
				}
				$("#ulList").append(ul);
				
			}
 
			$("#divPageNav").html(j.data.pageNav);
			$("#total").html(j.data.total);
		}
		});
}
function showDataPage(refreshData)
{
	//$("#chkSelAll")[0].checked=false;
	gIsEditSubject = false;
	gIsEditSubjectOnly = false;
	setHeaderPos("素材上传");
	$("#tblDetail").hide();
	$("#tblData").show();
	$("#id").val("0");
	$("#pdid").val("0");
	//$("#selSt").val(0);
	//$("#selSt").change();
	//getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
	if(refreshData)
	{
		lstDir();
		showStFileCount(curDirId,curDirSid);
	}
	//showStFileCount(curDirId,curDirSid);
}
//////////////////////////////
var gId = 0;
function toModifyPage()
{
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
		 }
		 });
      
     if(id=="")
	 {
		 var msg = "您没有选择文件，是否要完善当前目录 \""+curDirName+"\" 所有文件的信息 包含所有主题下的所有文件及主题信息？";
		 if(curDirSname!="")
		 {
			 msg = "您没有选择文件，是否要完善当前目录 \""+curDirName+"\" 主题 \""+curDirSname+"\" 所有文件及主题的信息？";
		 }
		 if(confirm(msg))
		 {
			 //gid = 0;
			 //$("#id").val(curDirSid);
			 gIsEditSubject = true;
			 showDetail(curDirSid);
			 return;
		 }
		 else
		 {
			 return;
		 }
		 gId = curDirSid;
	 }
	 else
	 {
	 	//alert(id);
	 	gId = id.substring(0,id.length-1);
	 	//alert(gIds);
	 }
	 showDetail(gId);
	 
}
function showSubjectDetail(t)
{
	$( "#dialog-form-add-subject" ).dialog("close");
	if(t==1)
	{
		gIsEditSubjectOnly = true;
		gIsEditSubject = false;
	}
	else
	{
		gIsEditSubject = true;
		gIsEditSubjectOnly = false;
	}
	showDetail(curDirSid);
}
function showDetail(id)
{
	$("#tblData").hide();
	$("#tblDetail").show();
	
	garrt = new Array();
	garrc = new Array();
	garrtId = new Array();
	garrcId = new Array();
	gId = id;
	if(gId!="" && (!isNaN(gId)))
	{
		 
		$("#id").val(id);
		$("#fid").val(id);
		getDetailInfo();
	}
	else
	{
		if(gId=="")
		{
			$("#id").val("0");
			$("#fid").val("0");
		}
		else
		{
			$("#id").val(id);
			$("#fid").val(id);
		}
		getDetailInfo();
		$("#tdShowImg").css("background-image","url(images/more.jpg)");
		$("#tdShowImg").css("background-position","center");
		$("#tdShowImg").css("background-repeat","no-repeat");
		$("#btnDetailUnPost").hide();
		//$("#tdShowImg").html("多文件信息完善");
		$("#idid").html("多文件信息完善");
		setHeaderPos("多文件信息完善 &nbsp;&nbsp;<a href='javascript:showDataPage(false);'>返回</a>");
		 $("#btnUploadPrevPic").hide();
		showFileTypes(garrt,garrc);
		initDetailPage();
	}
}
var garrt=new Array(),garrc=new Array();
var garrtId=new Array(),garrcId=new Array();
function getDetailInfo()
{
    	$("#pdid").val(curDirId);
		$("#sid").val(curDirSid);
		var isSub = 0;
		//alert(gIsEditSubjectOnly);
		if(gIsEditSubject)
		{
			isSub = 1;
			$("#isUpdateAll").val("1");	 
		}
		else if(gIsEditSubjectOnly)
		{
			isSub = 1;
			$("#isUpdateAll").val("4");	  //只更新主题信息
		}
		else{
			$("#isUpdateAll").val("-1");
		}

		var more = gId;
		$.get("?act=ajax&op=getFileEditInfo&id="+gId+"&isSub="+isSub+"&sid="+curDirSid,function(data){
		 var j = eval("("+data+")");
		if(j.st=="ok")
		{
	       var showHeader = "多文件批量修改";
			if(gId.toString().indexOf(",")==-1)  //一个文件
			{
				
			if(typeof(j.data.path)!="undefined" && j.data.path!=null)
			{
				var showPicPath = j.data.path;
				if(j.data.fileTypeId=="39")
				{
				if(showPicPath.indexOf("s.")!=-1)
				{
					showPicPath = showPicPath.replace("s.","sb.");
				}
				else
				{
					showPicPath = showPicPath.replace(".","sb.");
				}
				//alert(showPicPath);
				}
				
				$("#tdShowImg").css("background-image","url("+showPicPath+")");
				$("#tdShowImg").css("background-position","center");
				$("#tdShowImg").css("background-repeat","no-repeat");
			}
				 showHeader = j.data.mark;
			}
			else  //多个文件
			{
				j.data.isPic=1;
				showHeader =  "多文件批量修改";
			}

			 
			//$("#tdShowImg").css("background-size","contain");
			$("#author").val(j.data.author);
			//alert($("#name").length());
			$("#mark").val(j.data.mark);
		    //alert($("#pdid").val());
			//alert( $("#id").val());
            $("#idid").html(gId);
				
				if(gIsEditSubject)
				{
				     showHeader = "批量修改主题 "+curDirSname + " 下的所有文件信息";
					 $("#btnUploadPrevPic").hide();
					 $("#tdShowImg").css("background-image","url(images/more.jpg)");
					 $("#tdShowImg").css("background-position","center");
					 $("#tdShowImg").css("background-repeat","no-repeat");
				}
				else
				{
			if(j.data.isPic==1)
			{
				$("#btnUploadPrevPic").hide();
			}
			else
			{
				$("#btnUploadPrevPic").show();
			}
				}
				if(showHeader==null)
				{
					showHeader = j.data.name;
				}
                
				if(getQueryString("showEdit")!=null)
				{
					setHeaderPos(showHeader);
				}
				else
				{
					setHeaderPos(showHeader+" &nbsp;&nbsp;<a href='javascript:showDataPage(false);'>返回</a>");
				}
				
			
			$("#instro").val(j.data.instro);
			$("#uploader").html(j.data.uname);
			$("#zhuanTi").val(j.data.zhuanTi);
			$("#limitUse").val(j.data.limitUse);
			$("#size").append(j.data.size);
			$("#keywords").val(j.data.keywords);
			$("#avaliDate").val(j.data.avaliDate);
			$("#info").html(j.data.fileTypeName);
			$("#brand").attr("sel",j.data.brandId);
			$("#car").attr("sel",j.data.carId);
			$("#type").attr("sel",j.data.typeId);
			$("#style").attr("sel",j.data.styleId);
			if(j.data.year==null)
			{
				$("#year").attr("sel",new Date().getFullYear());
				$("#month").attr("sel",new Date().getMonth()+1);
			}
			else
			{
				var ym  = j.data.year.split("-");
 
				$("#year").attr("sel",ym[0]);
				if(ym.length==2)
				{
					$("#month").attr("sel",ym[1]);
				}
				else
				{
					$("#month").attr("sel","-1");
				}
				
			}
			//$("#brandName").attr("sel",j.data.brandName);
			//$("#carName").attr("sel",j.data.carName);
			//$("#typeName").attr("sel",j.data.typeName);
			//$("#styleName").attr("sel",j.data.styleName);
			
			$("#tblSelCar").html("");
			$("#tblSelType").html("");
			$("#dirTypes").html("—内容类型&nbsp;(0)");
			$("#dirCars").html("—品牌车型&nbsp;(0)");
			//alert(j.data.isPost);
			$("#btnDetailUnPost").hide();
			$("#btnDetailSave").show();
			if(j.data.isPost==0)
			{
				//$("#btnDetailPost").show();
				$("#btnDetailUnPost").hide();
			}
			else if(j.data.isPost==1)
			{
				$("#btnDetailPost").hide();
				$("#btnDetailUnPost").show();
			}
			else if(j.data.isPost==2)
			{
				$("#btnDetailPost").hide();
				$("#btnDetailUnPost").hide();
			}
 
			try
			{
            if(j.data.typeNames!=null)
			{
				garrt = j.data.typeNames.split(",");
				garrtId = j.data.typeIds.split(",");
				
			}
			if(j.data.carNames!=null)
			{
				garrc = j.data.carNames.split(",");
				garrcId = j.data.carIds.split(",");
			}
			 
			showFileTypes(garrt,garrc,garrtId,garrcId);
			}catch(e){}
			initDetailPage();
		}
		else
		{
			//window.location.href="index.html";
		}
		 	 
		 });
}

function showFileTypes(arrt,arrc,arrtId,arrcId)
{
 
	$("#tblSelType").html("");
	$("#dirTypes").html("—内容类型&nbsp;(0)");
	if(arrt.length>0)
	{
 
		var jx = 0;
		for(i=0;i<arrt.length;i++)
		{
			var t = arrt[i];
			var tid = arrtId[i];
			if(t.indexOf(":")!=-1)
			{
			var n = t.split(":");
			var nid = tid.split(":");
			$("#tblSelType").append('<tr data="'+nid[0]+":"+nid[1]+'"><td style="padding:0; border:none;width:200px; padding-left:20px;">'+n[0]+' '+ n[1]+'</td><td style="font-size:20px;padding:0; border:none; cursor:pointer" onclick="delFileType(1,'+i+')">x</td></tr>');
			jx++;
			}
		}
		$("#dirTypes").html("—内容类型&nbsp;("+jx+")");
	}
	$("#tblSelCar").html("");
	$("#dirCars").html("—品牌车型&nbsp;(0)");
	if(arrc.length>0)
	{
		var j = 0;
		for(i=0;i<arrc.length;i++)
		{
			var t = arrc[i];
			var tid = arrcId[i];
			if(t.indexOf(":")!=-1)
			{
			var n = t.split(":");
			var nid = tid.split(":");
			$("#tblSelCar").append('<tr data="'+nid[0]+":"+nid[1]+'"><td style="padding:0; border:none;width:200px; padding-left:20px;">'+n[0]+' '+ n[1]+'</td><td style="font-size:20px;padding:0; border:none;cursor:pointer"  onclick="delFileType(2,'+i+')">x</td></tr>');
			j++;
			}
		}
		$("#dirCars").html("—品牌车型&nbsp;("+j+")");
	}
}
function delFileType(t,i)
{
	//$.get("?act=upload&op=removeFileDirType&id="+$("#id").val()+"&type="+t+"&tid="+i,function(d)
	//{
	//	var j = eval("("+d+")");
	//	if(j.st=="ok")
	//	{
	//		 getDetailInfo();
	//	}
	//})
	if(t==2)
	{
		$($("#tblSelCar").find("tr")[i-1]).remove();
		$("#dirCars").html("—品牌车型&nbsp;("+$("#tblSelCar").find("tr").length+")");
	}
	else
	{
		$($("#tblSelType").find("tr")[i-1]).remove();
		$("#dirTypes").html("—内容类型&nbsp;("+$("#tblSelType").find("tr").length+")");
	}
}
 
function initDetailPage()
{

		$("#year").html("<option value='0'>选择</option>");
		for(i=2013;i<new Date().getFullYear()+2;i++)
		{
			$("#year").append("<option>"+i+"</option>");
		}
		
        initSelect("brand",$("#brand").attr("did"));
		initSelect("type",$("#type").attr("did"));
		//initSelect("style",$("#style").attr("did"));
		$("#style").html("<option value='0'>选择</option>");
		$("#car").html("<option value='0'>选择</option>");
		$("#brand").change(function(){
			$("#brandName").val($("#brand").find("option:selected").text());
		    initSelect("car",$("#brand").val());
			$("#carName").val("");
		});
		$("#car").change(function(){
			$("#carName").val($("#car").find("option:selected").text());
		});
		$("#style").change(function(){
			$("#styleName").val($("#style").find("option:selected").text());
			
		});
		$("#type").change(function(){
		    initSelect("style",$("#type").val());
			$("#typeName").val($("#type").find("option:selected").text());
			$("#styleName").val("");
		});
		$("#avaliDate").datepicker();

		if($("#year").attr("sel")=="0")
		{
			$("#year").val(new Date().getFullYear());
		}
		else
		{
			$("#year").val($("#year").attr("sel"));
		}
		if($("#month").attr("sel")=="0")
		{
			$("#month").val(new Date().getMonth()+1);
		}
		else if($("#month").attr("sel")=="-1")
		{
			$("#month").val("0");
		}
		else {
			$("#month").val($("#month").attr("sel"));
		}
}
function initSelect(obj,id)
{
	$.get("?act="+$("#act").val()+"&op=ajaxType&id="+id,function(data){
		var json = eval("("+data+")");		   
		if(json.length>0)
		{
			selop = "<option value='0'>选择</option>";
			for(i=0;i<json.length;i++)
			{
				selop = selop+"<option value='"+json[i].id+"'>"+json[i].name+"</option>";
			}
			//alert(id);
			$("#"+obj).html(selop);
		}
		else
		{
		    $("#"+obj).html("<option value='0'>无选项 不用选择</option>");
		}
		//if($("#"+obj).attr("sel")!="")
		//{
			//$("#"+obj).val($("#"+obj).attr("sel"));
			//$("#"+obj).change();
		//}
		//if(obj=="brand")
		//{
		    //$("#"+obj).val("2");
			//$("#brandName").val($("#brand").find("option:selected").text());
			//initSelect("car",$("#brand").val());
		//}
	});
}
function saveResult(st,t)
{
 
	if(st==1)
	{
		if(t=="post")
		{
			alert("发布成功");
		}
		if(t=="unpost")
		{
			alert("下架成功");
			showDataPage(true);
		}
		else if(t=="save")
		{
			//alert("保存成功");
 
			if(getQueryString("showEdit")==null)
			{
				if($("#roleId").val()=="2")
				{
					headerShowTips("信息保存成功，现在是否立即选择文件加入待审批列表？",2);
				}
				else
				{
					alert("保存成功");
					showDataPage(true);
				}
			}
			else
			{
				alert("保存成功");
			}
		}
		//showDataPage();
	}
	else if(st==0)
	{
		alert("专题已经存在");
	}
}
function saveTypeResult(st)
{
	if(st==1)
	{
		//alert("发布成功111");
		//showDataPage();
		//getDetailInfo();
		var isChange = false;
		if($("#brandName").val()!="")
		{
			var isExist = false;
			var line = $("#brandName").val()+":"+$("#carName").val();
			for(i=0;i<garrc.length;i++)
			{
				if(garrc[i]==line)
				{
					isExist =true;
					break;
				}
			}
			if(!isExist)
			{
				garrc.push(line);
				isChange = true;
			}
		}
		if($("#typeName").val()!="")
		{
			var isExist = false;
			var line = $("#typeName").val()+":"+$("#styleName").val();
			for(i=0;i<garrt.length;i++)
			{
				if(garrt[i]==line)
				{
					isExist =true;
					break;
				}
			}
			if(!isExist)
			{
				garrt.push(line);
				isChange = true;
			}
		}
		if(isChange)
		{
			showFileTypes(garrt,garrc);
		}
	}
}
 function newUploader(button)
 {
 	var uploader = new plupload.Uploader({
	runtimes : 'html5,flash,silverlight,html4',
	browse_button : $("#"+button).find("#btnUploadPrevPic")[0], // you can pass in id...
	container: document.getElementById('users-contain'), // ... or DOM Element itself
	url : 'upload.php',
	flash_swf_url : 'js/Moxie.swf',
	silverlight_xap_url : 'js/Moxie.xap',
	chunk_size:"1mb",
	filters : {
		max_file_size : '2000mb',
		mime_types: [
			{title : "Image files", extensions : "jpg"}
		]
	},

	init: {
		PostInit: function() {
			document.getElementById('filelist').innerHTML = '';

			//document.getElementById('uploadfiles').onclick = function() {
			//	uploader.start();
			//	return false;
			//};
		},

		FilesAdded: function(up, files) {
			plupload.each(files, function(file) {
				//document.getElementById(status).innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
				uploader.start();
				$("#"+button).find("p").show();
				//document.getElementById('filelist').innerHTML += '<table width="100%" class="tblFile" id="tblFile_'+file.id +'"><tr><td width="10" rowspan="2">&nbsp;</td><td width="350">'+file.name+'</td><td width="100">'+plupload.formatSize(file.size)+'</td><td  width="223" rowspan="2">&nbsp;</td></tr><tr><td colspan="2" id="tdProess_'+file.id +'">&nbsp;</td></tr></table><br/>'
			});
		},

		UploadProgress: function(up, file) {
			//document.getElementById("tdProess_"+file.id).innerHTML = '<span>' + file.percent + "%</span>";
			//document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
			$("#"+button).find("p").html(file.percent+"%");

		},
        FileUploaded:function(up,file,o)
		{
			var json = eval("("+o.response+")");
			$("#"+button).find("p").html("正在处理...");
			//alert(json.path);
			//$("#"+button).css("background-image","url("+j.data.path.replace("s.","sb.")+")");
			//$("#"+button).find("img")[0].src = json.path;
			//$("#"+button).find("#imgPath").val(json.path);
			$.post("?act=upload&op=saveFileImg",{name:file.name,path:json.path,edit:gId},function(data){
				//alert(data);
				var j = eval("("+data+")");
				$("#"+button).find("p").hide();
				if(j.st=="ok")
				{
					$("#"+button).css("background-image","url("+j.path+"?"+new Date().getTime()+")");
				}
				else
				{
					alert("处理失败，稍候重试");
				}
				});
		},
		UploadComplete:function(up,o)
		{
			 
		},
		Error: function(up, err) {
			document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
		}
	}
});
 uploader.init();
 return uploader;
}
function showStFileCount(id,sid)
{
	$("#chkSelAll")[0].checked = false;
	$.get("?act=upload&op=getStFileCount&did="+id+"&sid="+sid,function(d){
		var j = eval("("+ d+ ")");
		$("#selSt").html("");
		c = 0;
        for(i=0;i<j.length;i++)
		{
			$("#selSt").append("<option value='"+j[i].v+"'>"+j[i].n+"("+j[i].c+")</option>");
			c = c+ parseInt(j[i].c);
		}
        if(curDirSname!="")
		{
			$("#title").html("目录："+curDirName+" / 主题："+curDirSname+" ( "+c+" ) ");
		}
		else
		{
			$("#title").html("目录："+curDirName+" ( "+c+" ) ");
		}
		//gSt = 1;
		$("#selSt").val(gSt);
		$("#selSt").change();
	});
	
}